package com.xiaomaoguai.groovy;

import groovy.lang.GroovyClassLoader;

/**
 * @fileName: GlueFactory.java
 * @author: WeiHui.Zhang
 * @date: 2016-11-16  15:57
 * @version: v1.0.0
 */
public class GlueFactory {

	private GroovyClassLoader groovyClassLoader = new GroovyClassLoader();

	private String javaCode = "package com.xiaomaoguai.groovy;\n" +
			"\n" +
			"/**\n" +
			" * @fileName: Test.java\n" +
			" * @author: WeiHui.Zhang\n" +
			" * @date: 2016-11-16  16:00\n" +
			" * @version: v1.0.0\n" +
			" */\n" +
			"public class Test implements SayHello {\n" +
			"\n" +
			"\t@Override\n" +
			"\tpublic void Say() {\n" +
			"\t\tSystem.out.println(\"hello wolrd , Groovy classLoad !\");\n" +
			"\t}\n" +
			"\n" +
			"\t@Override\n" +
			"\tpublic void Say(String name) {\n" +
			"\t\tSystem.out.println(\"hello wolrd \" + name + \" , Groovy classLoad !\");\n" +
			"\t}\n" +
			"}\n";

	public static void main(String[] args) throws Exception {
		GlueFactory factory = new GlueFactory();

		Class<?> clazz = factory.groovyClassLoader.parseClass(factory.javaCode);
		if (clazz != null) {
			System.out.println(clazz);
			Object instance = clazz.newInstance();
			if (instance instanceof SayHello) {
				SayHello sayHello = (SayHello) instance;
				sayHello.Say();
			}
		}
	}
}
